package com.baichang.taoshunda.dao.impl;

import com.baichang.framework.dao.impl.BaseDaoImpl;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.dao.OrderDetailsDao;
import com.baichang.taoshunda.entity.OrderDetails;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class OrderDetailsDaoImpl extends BaseDaoImpl<OrderDetails> implements OrderDetailsDao {


    /**
     * 根据订单编号获取订单详情信息（商品）
     *
     * @param orderNumber 订单编号
     * @return
     */
    @Override
    public List<Map<String, Object>> findOrderDetailsByOrderNumber(String orderNumber) {
        String sql = "SELECT gd.id AS goodsId, gd.head_pic AS headPic, gd.company_id AS companyId, gd.`name` AS goodsName," +
                " gd.price , odd.goods_spec AS goodsSpec, odd.order_number,odd.id AS orderDetailId, odd.count," +
                "odd.is_no_reason_return, odd.univalent ,  odd.status , ( SELECT SUM(count) " +
                "FROM t_order_details od WHERE od.order_number = ? ) AS allNumber, gd.is_free FROM t_order_details odd " +
                "INNER JOIN t_goods gd ON odd.goods_id = gd.id WHERE odd.order_number = ? ";
        return queryForMapList(sql, orderNumber, orderNumber);
    }

    @Override
    public List<Map<String, Object>> getGoodsList(String orderNumber) {
        String sql = "SELECT * FROM t_order_details WHERE order_number=?";
        return queryForMapList(sql, orderNumber);
    }

    /**
     * @param orderNumber
     * @param nowPage
     * @param pageSize    @return
     */
    @Override
    public Page businessOrderDetail(String orderNumber, int nowPage, int pageSize) {
        String sql = "SELECT * FROM t_order_details WHERE order_number=?";
        return pageForBeanList(sql, nowPage, pageSize, orderNumber);
    }

    @Override
    public Page getOrderListByGoodsId(int nowPage, int pageSize, int id, String cstart, Integer jyjlzffs) {
        List list = new ArrayList();
        String sql = " SELECT goods.id, goods.order_number, goods.goods_id, goods.univalent, goods.count , goods.created, goods.goods_name, goods.unit, o.all_money, o.`order_name` AS businessName , o.`name` AS userName,o.pay_type,o.order_state FROM t_order_details goods INNER JOIN t_order o ON o.order_number = goods.order_number WHERE goods.goods_id = ?";
        list.add(id);
        if (jyjlzffs != -1) {
            sql += " AND o.pay_type= ?";
            list.add(jyjlzffs);
        }
        if (!cstart.equals("")) {
            sql += " AND TO_DAYS(goods.created)=TO_DAYS(?)";
            list.add(cstart);
        }
        sql += " ORDER BY o.created DESC";
        return pageForMapList(sql, nowPage, pageSize, list.toArray());
    }

    @Override
    public boolean updateStatus(int orderDetailId, int status) {
        String sql = "UPDATE `t_order_details` SET `status`=? WHERE id= ?";
        return update(sql, status, orderDetailId) > 0;
    }

    @Override
    public void updateStatusByOrderNumber(String orderNumber, int status) {
        String sql = "UPDATE `t_order_details` SET `status`=? WHERE order_number=? AND status=2";
        update(sql, status, orderNumber);
    }

    @Override
    public BigDecimal getAllMoney(String orderNumber) {
        String sql = "SELECT SUM(univalent*count) allMoney FROM t_order_details WHERE order_number=? ";
        return jdbcTemplate.queryForObject(sql, BigDecimal.class, orderNumber);
    }

    @Override
    public BigDecimal getReturnMoney(String orderNumber, int status) {
        String sql = "SELECT IFNULL(SUM(univalent*count),0) allMoney FROM t_order_details WHERE order_number=? AND `status`=? and is_return=0 ";
        return jdbcTemplate.queryForObject(sql, BigDecimal.class, orderNumber, status);
    }

    @Override
    public int getExtraction(Integer id) {
        String sql = "SELECT COUNT(0) FROM t_order_state nd WHERE nd.order_id=? AND nd.order_state=4";
        return queryForInt(sql, id);
    }

    @Override
    public OrderDetails getGoodsByOrderNumber(String orderNumber, int status) {

        String sql = "SELECT * FROM t_order_details WHERE `status`=? AND order_number=?  LIMIT 1";
        return queryForBean(sql, status, orderNumber);
    }

    @Override
    public int getGoodsNUmberByOrderNumber(String orderNumber) {
        String sql = "SELECT count(0) FROM t_order_details WHERE `status`=1 AND order_number=? ";
        return queryForInt(sql, orderNumber);
    }

    @Override
    public int getNoReturnCount(String orderNumber) {
        String sql = "SELECT COUNT(0) FROM t_order_details WHERE `status`!=3 AND order_number=? ;";
        return queryForInt(sql, orderNumber);
    }

    @Override
    public void updateIsReturn(String orderNumber) {
        String sql = "UPDATE `t_order_details` SET `is_return`='1' WHERE `status`=3 AND order_number= ?;";
        update(sql, orderNumber);
    }

    @Override
    public boolean updateStatusByOrderDetailsId(Integer orderDetailId, int i) {
        String sql = "UPDATE `t_order_details` SET `status`=? WHERE id=? AND status=2";
        return update(sql, i, orderDetailId) > 0;
    }


}